<%@page import="java.text.DecimalFormat"%>
<%@page import="org.proddevplm.dao.data.correlations.QFD"%>
<%@page import="org.proddevplm.dao.data.activities.Activity"%>
<%@page import="java.util.List"%>
<%@page import="org.proddevplm.constants.ControllerConstants"%>
<%@page import="org.proddevplm.business.tools.constants.ToolsConstants" %>
<%@page import="org.proddevplm.business.activity.constants.ActivityConstants" %>

<script>
	var qfdType = "";
	
	$(function(){
		$("button").button();
		$("#allQFDContent").hide();
	})
	
	function loadQFDWSWithReq(request, page) {
        $.post(request, function(data) {
            $("#qfdWorkspace").load(page);
        });
	}
	
	function REQvsCTQ(){
		loadQFDWSWithReq("/proddev/controller?manager=dataAccessManager&method=35","content/project/devTools/workspace/correlations/reqsVsCtqs.jsp");
	}
	
	function CTQvsFunctions(){
		loadQFDWSWithReq("/proddev/controller?manager=dataAccessManager&method=36","content/project/devTools/workspace/correlations/ctqsVsFunctions.jsp");
	}
	
	function FunctionsVsDE(){
		loadQFDWSWithReq("/proddev/controller?manager=dataAccessManager&method=37","content/project/devTools/workspace/correlations/functionsVsDEs.jsp");
	}
	
	function SHvsDFX(){
		loadQFDWSWithReq("/proddev/controller?manager=dataAccessManager&method=38","content/project/devTools/workspace/correlations/shVsDFX.jsp");
	}
	
	function dfxVsGuidelines(){
		loadQFDWSWithReq("/proddev/controller?manager=dataAccessManager&method=39","content/project/devTools/workspace/correlations/dfxVsGuidelines.jsp");
	}
		
	function approveActivity(name){
		$("#activityName").val(name);
		document.forms['approveForm'].submit();
	}
	
	function rejectActivity(name){
		$("#activityName").val(name);
		$("#rejectDialog").dialog("open");
	}
	
	$( "#rejectDialog" ).dialog({
		autoOpen: false,
		height: 350,
		width: 350,
		modal: true,
		buttons: {
			"Propose change": function() {
				var name = document.getElementById("activityName").value;
				var arguments = "activityName="+name+"&"+buildParameter("changeSubject")+"&"+buildParameter("changeDetails");
				ajaxCall("toolsManager", "21", arguments, forward);
				$( this ).dialog( "close" );
				},
			Cancel: function() {
				$( this ).dialog( "close" );
			}
		},
		close: function() {
			$( this ).dialog( "close" );
		}
	});
	
	function forward(data){
		document.forms["rejectForm"].submit();
	}

</script>

<%
	List<Activity> activitiesForUser = (List)session.getAttribute("activitiesForUser");
	QFD qfdByActName = (QFD)session.getAttribute("qfdByActName");
%>

<div class="pageTitle">
	<div style="text-align:left;">
		<a href="/proddev/controller?manager=dataAccessManager&method=21">
			<img src="/proddev/images/arrowleft.png" align="middle" height="20" width="30" style="border:0px"></a>
	</div>
	<p style="font-size:small; font-weight:bold; margin-top:-20px">Correlation Matrix</p>

</div>

<%if(qfdByActName==null){ %>
	<div style="margin:10 0 0; width:100%">
		<div style="width:30%; float:left; text-align:left; font-weight:bold; color:#555555">
			Please choose the activity you want to complete
		</div>
		<select class="regInput" id="activitySelect" name="activitySelect" onchange="$('#allQFDContent').show()" size="1" style="width:25%; float:left; margin-left:10px">
			<option SELECTED>-select activity-</option>
			<%if(activitiesForUser!=null){
				for(int i=0; i<activitiesForUser.size(); i++) {
					Activity activity = (Activity)activitiesForUser.get(i);%>
					<option value="<%=activity.getName()%>"><%=activity.getName() %></option>
				<%}
			}
			%>		
		</select>
		<div style="clear:both"></div>
	</div>

	<div id="allQFDContent" style="width:100%; margin-top:10px;">
		<div id="leftContent" style="width:20%; float:left;">
			<button id="REQvsCTQ" name="REQvsCTQ" onclick="REQvsCTQ()" style="width:60%; margin-top:10px; font-size:x-small; font-weight:bold; float:center">Requirements vs. CTQs</button>
			<button id="CTQvsFunctions" name="CTQvsFunctions" onclick="CTQvsFunctions()" style="width:60%; margin-top:10px; font-size:x-small; font-weight:bold; float:center">CTQs vs. Functions</button>
			<button id="FunctionsVsDE" name="FunctionsVsDE" onclick="FunctionsVsDE()" style="width:60%; margin-top:10px; font-size:x-small; font-weight:bold; float:center">Functions vs. Design Elements</button>
			<button id="SHvsDFX" name="SHvsDFX" onclick="SHvsDFX()" style="width:60%; margin-top:10px; font-size:x-small; font-weight:bold; float:center;">Stakeholders vs. DFX concepts</button>
			<button id="dfxVsGuidelines" name="dfxVsGuidelines" onclick="dfxVsGuidelines()" style="width:60%; margin-top:10px; font-size:x-small; font-weight:bold; float:center;">DFX concepts vs. DFX Guidelines</button>
			<div style="clear:both"></div>
			<div class="regInput" style="width:90%; margin-top:20px; margin-bottom:20px; margin-left:auto; margin-right:auto; text-align:left; background-color:#F0F0F6; -moz-box-shadow: 10px 10px 5px #888888; border: 1px solid #DBDBDB; color:#555555">
				<p style="font-weight:bold;">Please use the following correlation scale</p>
				<p>++ - 9: very strong correlation</p>
				<p>+ - 3: some correlation</p>
				<p>O - 1: possible correlation</p>
				<p>- - 0: no correlation</p>
			</div>
		</div>
		<div id="rightContent" style="width:79%; float:left; overflow:auto;">
			<div id="qfdWorkspace" style="width:100%"></div>
		</div>	
		<div style="clear:both"></div>	
	</div>
<%}else{
	String hows = qfdByActName.getHows();
	String[] howsArray = hows.split("~");
	String whats = qfdByActName.getWhats();
	String[] whatsArray = whats.split("~");
	String results = qfdByActName.getResults();
	String[] resultsArray = results.split("~");
	
	String matrixValues = qfdByActName.getMatrixValues();
	String[] matrix = matrixValues.split("~");
	String[] symbols = new String[matrix.length];
	for(int i=0; i<matrix.length; i++){
		if(Integer.valueOf(matrix[i]) == 9){
			symbols[i] = "++";
		}else if(Integer.valueOf(matrix[i]) == 3){
			symbols[i] = "+";
		}else if(Integer.valueOf(matrix[i]) == 1){
			symbols[i] = "O";
		}else if(Integer.valueOf(matrix[i]) == 0){
			symbols[i] = "-";
		}
	}
	
	String[][] qfdMatrix = new String[whatsArray.length][howsArray.length];
	
	int l=0; int m=0;
	int cont = 0;
	for(int index=0; index<symbols.length; index++){
		
		String value = symbols[index];
		if(cont==(howsArray.length-1)){
			qfdMatrix[l][m] = value;
			l++;
			m=0;
			cont=0;
		}else{
			qfdMatrix[l][m] = value;
			m++;
			cont++;
		}
	}
	
	%>
	<div align="center" style="width:60%; margin-top:10px; margin-left:auto; margin-right:auto">
		<table id="qfdTable" border="1" cellspacing="0" cellpadding="3" style="width:100%; font-size:small; ">
			<tr id="headerUp" style="height:80px; background-color:#F0F0F6">
				<td style="font-weight:bold; color:#555555">Hows</td>
				<%for(int i=0; i<howsArray.length; i++){%>
					<td rowspan="2" style="padding:0"><span class="rotatedText" style="margin-bottom:0px; margin:-20px">
						<%=howsArray[i] %>
						</span>
					</td>
				<%} %>
			</tr>
			<tr id="headerDown" style="height:80px; background-color:#F0F0F6">
				<td style="font-weight:bold; color:#555555">Whats</td>
			</tr>
			<%for(int i=0; i<whatsArray.length; i++){ %>
				<tr>
					<td><%=whatsArray[i] %></td>
					<%for(int j=0; j<howsArray.length; j++){ %>
						<td style="width:10%; text-align:center">
							<%=qfdMatrix[i][j] %>
						</td>
					<%} %>
				</tr>
			<%} %>
		</table>
		
		<table id="qfdResults" border="1" cellspacing="0" cellpadding="3" style="width:100%; margin-top:40px; font-size:small; ">
			<tr style="background-color:#F0F0F6">
				<td style="width:75%; font-weight:bold; color:#555555">Elements</td>
				<td style="width:25%; font-weight:bold; color:#555555">Importance</td>
			</tr>
			<%for(int i=0; i<howsArray.length; i++){ %>
				<tr>
					<td><%=howsArray[i] %></td>
					<td>
						<%Float imp = Float.valueOf(resultsArray[i]);
						DecimalFormat dFormat = new DecimalFormat("0.00");
						String shownVal = dFormat.format(imp)+"%";%>
						<%=shownVal %>
					</td>
				</tr>
			<%} %>		
		</table>
		
		<%String activityName = qfdByActName.getActivityName();%>
		<div style="width:100%; margin-top:10px">
			<button id="rejectActivity" name="rejectActivity" onclick="rejectActivity('<%=activityName%>')" style="width:150px; font-weight: bold; float:left">Reject activity</button>
			<button id="approveActivity" name="approveActivity" onclick="approveActivity('<%=activityName%>')" style="width:150px; font-weight: bold; float:right">Approve activity</button>
			<div style="clear:both"></div>
		</div>
			
		<div id="rejectDialog" title="Propose changes" style="display:none">
			<jsp:include page="/pages/content/project/devTools/workspace/changes/changeReport.jsp"></jsp:include>
		</div>
		
		<form name="rejectForm" method="post" action="/proddev/controller" style="margin:0px">
			<input type = "hidden" name = "<%=ControllerConstants.MANAGER_KEY%>" value = "<%=ControllerConstants.TOOLS_MANAGER%>"/>
			<input type = "hidden" name = "<%=ControllerConstants.METHOD_KEY%>" value = "<%=ToolsConstants.STAKEHOLDERS_FWD%>"/>
		</form>
			
		<form name="approveForm" method="post" action="/proddev/controller" style="margin:0px">
			<input type = "hidden" name = "<%=ControllerConstants.MANAGER_KEY%>" value = "<%=ControllerConstants.ACTIVITY_MANAGER%>"/>
			<input type = "hidden" name = "<%=ControllerConstants.METHOD_KEY%>" value = "<%=ActivityConstants.UPDATE_ACTIVITY%>"/>
			<input type="hidden" name="activityName" id="activityName">
		</form>
		<div style="clear:both"></div>
	</div>
<%}%>